package com.jsoft.pds.view.beans.authentication;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.servlet.http.HttpSession;

import com.jsoft.pds.view.beans.base.BaseBean;
import com.jsoft.pds.view.beans.navigation.NavigationBean;

@ManagedBean(name = "credentialsBean")
@SessionScoped
public class CredentialsBean extends BaseBean {
	
	private static final long serialVersionUID = -6791377267590150308L;

	public static final String REDIRECT_LOGIN = "dashboard";
	public static final String REDIRECT_LOGOUT = "index";
	public static final String REDIRECT_CHANGE_PASSWORD = "";
	
	@ManagedProperty(value = "#{navigationBean}")
	protected NavigationBean navigationBean;

	public String authenticationAction(UserSessionBean userSessionBean){
		
		if(userSessionBean.getUsername().equals("admin") && userSessionBean.getPassword().equals("admin")){
			userSessionBean.setProfile("admin");
			userSessionBean.setPrimerNombre("Oswaldo");
			userSessionBean.setSegundoNombre("Emmanuel");
			userSessionBean.setPrimerApellido("López");
			userSessionBean.setSegundoApellido("Solórzano");
		}
		
		if(userSessionBean.getUsername().equals("recepcion") && userSessionBean.getPassword().equals("recepcion")){
			userSessionBean.setProfile("recepcion");
			userSessionBean.setPrimerNombre("Marta");
			userSessionBean.setSegundoNombre("Eugenia");
			userSessionBean.setPrimerApellido("Aguilar");
			userSessionBean.setSegundoApellido("Aguilar");
		}
		
		if(userSessionBean.getUsername().equals("caja") && userSessionBean.getPassword().equals("caja")){
			userSessionBean.setProfile("caja");
			userSessionBean.setPrimerNombre("Rigoberto");
			userSessionBean.setSegundoNombre("Enrique");
			userSessionBean.setPrimerApellido("Estrada");
			userSessionBean.setSegundoApellido("Cardona");
		}
		
		if(null != userSessionBean.getProfile()){
			userSessionBean.setRemoteIpAddres(super.getHttpServletRequest().getRemoteAddr());
			getNavigationBean().buildPanelMenu(userSessionBean);
			return REDIRECT_LOGIN; 
		}
		return null;
	}
	
	public String logoutAction(final UserSessionBean userSessionBean){
		try{
			HttpSession session = super.getHttpSession();
			session.invalidate();
			return REDIRECT_LOGOUT;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public void homeAction(final UserSessionBean userSessionBean){
		this.getNavigationBean().goHome();
	}
	
	public void changePasswordAction(final UserSessionBean userSessionBean){
		System.out.println("CAMBIAR PASSWORD " + userSessionBean.getUsername());
	}
	
	public void helpAction(final UserSessionBean userSessionBean){
		System.out.println("VER AYUDA " + userSessionBean.getUsername());
	}

	public NavigationBean getNavigationBean() {
		return navigationBean;
	}

	public void setNavigationBean(NavigationBean navigationBean) {
		this.navigationBean = navigationBean;
	}
	
}
